<?php
App::uses('AppModel', 'Model');
/**
 * NouveauStatut Model
 *
 * @property reponse $reponse
 * @property reponse $reponse
 * @property statuts_reponse $statuts_reponse
 */
class NouveauStatut extends AppModel {
	
	public function detruire_par_les_id($id_reponse, $id_reponse_statut_change_par) {
		$this->query("DELETE FROM nouveau_statuts WHERE id_reponse = ".$id_reponse." AND id_reponse_statut_change_par = ".$id_reponse_statut_change_par);
	}
	
	public function detruire_par_id_reponse($id_reponse) {
		$this->query("DELETE FROM nouveau_statuts WHERE id_reponse = ".$id_reponse);
	}
	public function detruire_par_id_reponse_statut_change_par($id_reponse_statut_change_par) {
		$this->query("DELETE FROM nouveau_statuts WHERE id_reponse_statut_change_par = ".$id_reponse_statut_change_par);
	}
	// permet de créer ou mettre à jour une action de réponse à réponse
	public function creer_ou_mettre_a_jour($id_reponse_affectant, $id_reponse_affectee, $id_nouveau_statut) {
		$result = $this->query("SELECT * FROM nouveau_statuts WHERE nouveau_statuts.id_reponse = ".$id_reponse_affectant." AND nouveau_statuts.id_reponse_statut_change_par=".$id_reponse_affectee);

		if(!$result)
			$this->query("INSERT INTO nouveau_statuts (id_reponse, id_reponse_statut_change_par, id_statut_reponse) VALUES (".$id_reponse_affectant.", ".$id_reponse_affectee .",".$id_nouveau_statut.")");
		else 
			$this->query("UPDATE nouveau_statuts SET id_statut_reponse=".$id_nouveau_statut." WHERE id_reponse =".$id_reponse_affectant." AND id_reponse_statut_change_par=".$id_reponse_affectee);
		
		$affectant = $this->query("SELECT * FROM reponses WHERE id_reponse = $id_reponse_affectant");
		$affectee = $this->query("SELECT * FROM reponses WHERE id_reponse = $id_reponse_affectee");
		
		return Array('affectant'=> $affectant, 'affectee' => $affectee);
	}
/**
 * Use table
 *
 * @var mixed False or table name
 */
	public $useTable = 'nouveau_statuts';
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_nouveau_statut';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_nouveau_statut' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_reponse' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_reponse_statut_change_par' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'id_statut_reponse' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'reponse' => array(
			'className' => 'reponse',
			'foreignKey' => 'id_reponse',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'reponse' => array(
			'className' => 'reponse',
			'foreignKey' => 'id_reponse',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'statuts_reponse' => array(
			'className' => 'statuts_reponse',
			'foreignKey' => 'id_statut_reponse',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
}
